package com.wanggy.tool;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import java.sql.PreparedStatement;
import java.sql.SQLException;

public class MysqlUtils {

    //connection
    private static  Connection connection;

    //preparedStatement
    private static PreparedStatement preparedStatement;


    // 获取preparedStatement
    public static PreparedStatement getPreparedStatement(String url, String username, String password, String sql){
        MysqlDataSource dataSource = new MysqlDataSource();
        dataSource.setURL(url);
        dataSource.setUser(username);
        dataSource.setPassword(password);
        try {
            //获取数据库链接
            connection = (Connection) dataSource.getConnection();
            preparedStatement = connection.prepareStatement(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return preparedStatement;
    }
}
